<?php
class AsistenciasController extends AppController {

	var $name = 'Asistencias';
	//var $components = array('RequestHandler');
	var $programacions=array();
	
	function index(){
		$this->Auth->login();
		$this->layout="ajax";	
		
		$asist= $this->Asistencia->getAsistencias();
		$this->set('asist', $asist);
		
		$this->loadModel('Tipopersona');
		$tipopersona= $this->Tipopersona->getTipopersonaLista();
		$this->set('tipopersona', $tipopersona);
		
		$this->autoRender = true;
	}
	
	function getCargarpersona(){
		
		configure::write('debug', 0);
		$this->layout = 'ajax';
		$this->loadModel('Persona');
		$art=$_POST['id'];
		//var_dump($art);exit;
		$articuloLista = $this->Persona->find('all',array(
				'fields'=>array('Persona.codigo','Persona.nombre_completo',
			'Persona.grupo',
			'Persona.subgrupo',
			'"Tipopersona"."descripcion" AS "Persona__descripcion"'),
				//'conditions'=>array('Persona.estado_id'=>1, '"Persona"."codigo" || \' - \'|| "Persona"."nombre_completo" ILIKE \'%'.trim($art).'%\''),
				'conditions'=>array('"Persona"."codigo" = \''.trim($art).'\''),
		));
	
		$this->set('persona', $articuloLista);
		
		//var_dump($articuloLista);exit;
	//	$this->autoRender = false;
		//return empty($tmp)?json_encode($tmp_1['Persona']):json_encode($tmp['Persona']);
	}
	
	
	
	function marcarasistencia()
	{
		//sadsadsad
		$this->layout="ajax";
		
		$this->data['Asistencia']['persona_id'] = $this->params['form']['id'];
		
		
		
		if ($this->Asistencia->asisteciaxdia($this->data['Asistencia']['persona_id']))
		{
			$this->data['Asistencia']['asistio'] = true;//$this->params['form']['asistio'];
		}
		else
		{
			$this->data['Asistencia']['asistio'] = false;//$this->params['form']['asistio'];
		}	
		
		$this->data['Asistencia']['estado_id'] = 1;
		
		
		$this->loadModel('Persona');
		
		$asistio= $this->Persona->find('all',
							array('conditions'=>
									array('Persona.codigo'=>$this->data['Asistencia']['persona_id'],
									)));
		
	
		if(!empty($asistio)){
				date_default_timezone_set('America/Lima');
				$this->data['Asistencia']['fecha'] = date('Y-m-d');	
				$this->data['Asistencia']['hora'] = date("H:i");;
				
				$this->data['Asistencia']['persona_id'] = $asistio[0]['Persona']['id'];
				
				$this->Asistencia->create();				 
				@$this->Asistencia->save($this->data['Asistencia']);
			}	
			
		$asist= $this->Asistencia->getAsistencias();
		$this->set('asist', $asist);
		
		$this->loadModel('Tipopersona');
		$tipopersona= $this->Tipopersona->getTipopersonaLista();
		$this->set('tipopersona', $tipopersona);
		//$this->autoRender = false;
		
	}
		
	
}